package service.contract.impl;

import java.util.List;

import model.TContract;
import model.company.Companybasic;
import model.company.Companycontact;
import model.company.Companypledger;
import model.company.Companytopmanager;
import model.contract.Contractensure;
import model.contract.Contractmortgage;
import model.contract.Contractpledge;
import model.contract.Loangrant;
import model.credit.Applycustomrelation;
import model.credit.Creditapplyenterprise;
import model.credit.Creditapplypersonal;
import model.dropdown.DCommon;
import model.dropdown.DIdtype;
import model.personal.Personalbasic;

import service.contract.ContractService;

import core.service.BaseService;
import dao.ApplycustomrelationDao;
import dao.CompanybasicDao;
import dao.CompanycontactDao;
import dao.CompanypledgerDao;
import dao.ContractensureDao;
import dao.ContractmortgageDao;
import dao.ContractpledgeDao;
import dao.CreditapplyenterpriseDao;
import dao.CreditapplypersonalDao;
import dao.CustomrelationDao;
import dao.LoangrantDao;
import dao.PersonalbasicDao;
import dao.TContractDao;
import dao.company.CompanytopmanagerDao;
import dao.dropdown.DCommonDao;
import dao.dropdown.DIdtypeDao;
import exception.Exceptions;


public class ContractServiceImpl extends BaseService implements ContractService {
	private CreditapplyenterpriseDao creditcompanyDao;
	private CreditapplypersonalDao creditpersonalDao;
	private TContractDao contractDao;
	private CompanybasicDao companyDao;
	private PersonalbasicDao personalDao;
	private CompanycontactDao contactDao;
	private CustomrelationDao relationDao;
	private ApplycustomrelationDao applyrelationDao;
	private ContractensureDao ensureDao;
	private CompanypledgerDao thingsDao;
	private ContractmortgageDao mortgageDao;
	private ContractpledgeDao pledgeDao;
	private LoangrantDao loangrantDao;
	private CompanytopmanagerDao topmanagerDao;
	private DCommonDao dcommonDao;
	
	//--===========对credit表的操作===========--//
	public List<?> get_credit_list(String custom,String credit,String customcode){
		String select="new vo.ContractCreateListBean(credit.applyid,custom.name,custom."+customcode+",credit.loanaccount)";
		String hql=credit+" as credit ,"+custom+" as custom where credit.clientid=custom.id and credit.creditstate=10";
		if(custom.equals("Companybasic")){
			return creditcompanyDao.findQuery(select,hql, 10);	
		}else{
			return creditpersonalDao.findQuery(select,hql, 10);
		}
	}
	public Creditapplyenterprise get_creditcompany(int creditid) throws Exceptions
	{
		Creditapplyenterprise creditcompany=creditcompanyDao.findOne("applyid", creditid);
		if(creditcompany==null)throw new Exceptions("数据获取异常，数据库没有申请信息记录！");
		return creditcompany;
	}
	public Creditapplypersonal get_creditpersonal(int creditid) throws Exceptions{
		Creditapplypersonal creditpersonal=creditpersonalDao.findOne("applyid",creditid);
		if(creditpersonal==null)throw new Exceptions("数据获取异常，数据库没有申请信息记录！");
		return creditpersonal;
	}
	public void save_in_creditPersonal(Creditapplypersonal creditpersonal){
		creditpersonalDao.save(creditpersonal);
	}
	public void save_in_creditCompany(Creditapplyenterprise creditcompany){
		creditcompanyDao.save(creditcompany);
	}
	
	//--==========对客户基本信息的操作=========--//
	public Companybasic get_companybasic(int clientid) throws Exceptions{
		Companybasic company=new Companybasic();
		company=companyDao.findOne("id",clientid);
		if(company==null) throw new Exceptions("数据获取异常，数据库中没有客户基本信息记录");
		return company;
	}
	public Companybasic get_companybasic(String name,String value) throws Exceptions{
		Companybasic company=new Companybasic();
		company=companyDao.findOne(name,value);
		if(company==null) throw new Exceptions("数据获取异常，数据库中没有客户基本信息记录");
		return company;
	}
	public Personalbasic get_personalbasic(int clientid) throws Exceptions{
		Personalbasic personal=personalDao.findOne("id",clientid);
		if(personal==null)throw new Exceptions("数据获取异常，数据库中没有客户基本信息记录");
		return personal;
	}
	public Personalbasic get_personalbasic(String name,String value) throws Exceptions{
		Personalbasic personal=personalDao.findOne(name,value);
		if(personal==null)throw new Exceptions("数据获取异常，数据库中没有客户基本信息记录");
		return personal;
	} 
	public Companycontact get_companycontact(int clientid) throws Exceptions{
		Companycontact contact=contactDao.findOne("cid",clientid);
		if(contact==null)throw new Exceptions("数据获取异常，数据库中没有客户联系信息记录");
		return contact;
	}
	
	//--============对合同表的操作===========--//
	public int save_in_contract(TContract contract){
		TContract c=new TContract();
		c=contractDao.save(contract);
		return c.getId();
	}
	public void save_in_contractensure(Contractensure contractensure){
		 ensureDao.saveOrUpdate(contractensure);
	}
	public void save_in_contractmortgage(Contractmortgage contractmortgage){
		mortgageDao.saveOrUpdate(contractmortgage);
	}
	public void save_in_contractpledge(Contractpledge contractpledge){
		pledgeDao.saveOrUpdate(contractpledge);
	}
	
	//--===========对applycustomrelation的操作======--//
	public List<?> show_ensureRelation(int creditid){
		
		String select=" applyrelation.id,applyrelation.applyid,applyrelation.relationpeopletype,"+
		"(case when applyrelation.relationpeopletype=1 then (select p1.name from personalbasic as p1 where p1.id=applyrelation.relationpeopleid)"+
		" when applyrelation.relationpeopletype=2 then (select c1.name from companybasic as c1 where c1.id=applyrelation.relationpeopleid)"+
		" else '0' end) as ensurename,"+
	   "(case when applyrelation.relationpeopletype=1 then(select p1.certificatenumber from personalbasic as p1 where p1.id=applyrelation.relationpeopleid)"+
		" when applyrelation.relationpeopletype=2 then (select c1.organizationcode from companybasic as c1 where c1.id=applyrelation.relationpeopleid)"+
		" else '0' end) as customcode,"+
		"(case when applyrelation.clienttype=1 then (select p2.name from personalbasic as p2 where p2.id=applyrelation.clientid)"+
		" when applyrelation.clienttype=2 then (select c2.name from companybasic as c2 where c2.id=applyrelation.clientid)"+
		" else '0' end) as customname";
		String from=" applycustomrelation as applyrelation where applyrelation.relationtype=3 and applyrelation.state='1'";
		if(creditid!=0){
		from=from+" and applyrelation.applyid="+creditid;
		}
		return applyrelationDao.findSqlQuery(select,from, 10);	    
	}
	public List<?> showRelation(int creditid,Integer type){
		String select=" applyrelation.id,applyrelation.applyid,applyrelation.relationpeopletype,"+
		"(case when applyrelation.relationpeopletype=1 then (select p1.name from personalbasic as p1 where p1.id=applyrelation.relationpeopleid)"+
		" when applyrelation.relationpeopletype=2 then (select c1.name from companybasic as c1 where c1.id=applyrelation.relationpeopleid)"+
		" else '0' end) as customname,"+
		"(case when applyrelation.relationpeopletype=1 then(select p1.certificatenumber from personalbasic as p1 where p1.id=applyrelation.relationpeopleid)"+
		" when applyrelation.relationpeopletype=2 then (select c1.organizationcode from companybasic as c1 where c1.id=applyrelation.relationpeopleid)"+
		" else '0' end) as customcode,"+
		"(case when applyrelation.clienttype=1 then (select p2.name from personalbasic as p2 where p2.id=applyrelation.clientid)"+
		" when applyrelation.clienttype=2 then (select c2.name from companybasic as c2 where c2.id=applyrelation.clientid)"+
		" else '0' end) as loanname,things.collateralname";
		String hql=" applycustomrelation as applyrelation,Companypledger as things where applyrelation.thingsid=things.id" +
				" and applyrelation.relationtype="+type+" and applyrelation.state='1'";
		if(creditid!=0){
			hql=hql+" and applyrelation.applyid="+creditid;
		}
		return applyrelationDao.findSqlQuery(select,hql, 10);			
	}
	public Integer get_countrelation(int creditid,Integer type){
	
		return applyrelationDao.countModel("applyid="+creditid+"and relationtype="+type+"and state='1'");
	}
	public Applycustomrelation getapplyrelation(String name,Integer value) throws Exceptions{
		Applycustomrelation customrelation=applyrelationDao.findOne(name, value);
		if(customrelation==null)throw new Exceptions("数据获取异常，数据库中没有保/抵/质关系记录");
		return customrelation; 
	}
	public void save_in_applycustomrelation(Applycustomrelation applyrelation){
		applyrelationDao.save(applyrelation);
	}
	
	//--============对customthings的操作=======--//
	public Companypledger get_customthings(int thingsid) throws Exceptions{
		Companypledger  things=thingsDao.findOne("id", thingsid);
		if(things==null) throw new Exceptions("数据获取异常，数据库中没有抵/质押物基本信息记录");
		return things;
	}
	public Companypledger get_customthings(String name,String value) throws Exceptions{
		Companypledger things=thingsDao.findOne(name, value);
		if(things==null)throw new Exceptions("数据获取异常，数据库中没有抵/质押物基本信息记录");
		return things;
	}
	
	//--==========对loangrant操作========--//
	public void save_in_loangrant(Loangrant loangrant){
		loangrantDao.save(loangrant);
	}
	
	//--==========对Companytopmanager======--//
	public Companytopmanager get_topmanager(String str) throws Exceptions{
		Companytopmanager topmanager=topmanagerDao.findByOne(str);
		if(topmanager==null) throw new Exceptions("数据获取异常，数据库中没有高管信息记录");
		return topmanager;
	}
	
	//--=============对DIdtype============--//
	public List<DCommon> getIdtypeList(){
		 return dcommonDao.findList("type", 2);
	}
//=============================================================================//	


	public TContractDao getContractDao() {
		return contractDao;
	}

	public void setContractDao(TContractDao contractDao) {
		this.contractDao = contractDao;
	}

	public CompanybasicDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(CompanybasicDao companyDao) {
		this.companyDao = companyDao;
	}

	public PersonalbasicDao getPersonalDao() {
		return personalDao;
	}

	public void setPersonalDao(PersonalbasicDao personalDao) {
		this.personalDao = personalDao;
	}

	public CompanycontactDao getContactDao() {
		return contactDao;
	}

	public void setContactDao(CompanycontactDao contactDao) {
		this.contactDao = contactDao;
	}

	public CustomrelationDao getRelationDao() {
		return relationDao;
	}

	public void setRelationDao(CustomrelationDao relationDao) {
		this.relationDao = relationDao;
	}

	public CreditapplyenterpriseDao getCreditcompanyDao() {
		return creditcompanyDao;
	}

	public void setCreditcompanyDao(CreditapplyenterpriseDao creditcompanyDao) {
		this.creditcompanyDao = creditcompanyDao;
	}

	public CreditapplypersonalDao getCreditpersoanlDao() {
		return creditpersonalDao;
	}

	public void setCreditpersonalDao(CreditapplypersonalDao creditpersonalDao) {
		this.creditpersonalDao = creditpersonalDao;
	}

	public ContractensureDao getEnsureDao() {
		return ensureDao;
	}

	public void setEnsureDao(ContractensureDao ensureDao) {
		this.ensureDao = ensureDao;
	}

	public ApplycustomrelationDao getApplyrelationDao() {
		return applyrelationDao;
	}

	public void setApplyrelationDao(ApplycustomrelationDao applyrelationDao) {
		this.applyrelationDao = applyrelationDao;
	}


	public ContractmortgageDao getMortgageDao() {
		return mortgageDao;
	}

	public void setMortgageDao(ContractmortgageDao mortgageDao) {
		this.mortgageDao = mortgageDao;
	}

	public ContractpledgeDao getPledgeDao() {
		return pledgeDao;
	}

	public void setPledgeDao(ContractpledgeDao pledgeDao) {
		this.pledgeDao = pledgeDao;
	}

	public LoangrantDao getLoangrantDao() {
		return loangrantDao;
	}

	public void setLoangrantDao(LoangrantDao loangrantDao) {
		this.loangrantDao = loangrantDao;
	}
	public CompanytopmanagerDao getTopmanagerDao() {
		return topmanagerDao;
	}
	public void setTopmanagerDao(CompanytopmanagerDao topmanagerDao) {
		this.topmanagerDao = topmanagerDao;
	}
	public CompanypledgerDao getThingsDao() {
		return thingsDao;
	}
	public void setThingsDao(CompanypledgerDao thingsDao) {
		this.thingsDao = thingsDao;
	}

	public DCommonDao getDcommonDao() {
		return dcommonDao;
	}
	public void setDcommonDao(DCommonDao dcommonDao) {
		this.dcommonDao = dcommonDao;
	}

}
